'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AdministratorAdminCgwzCtrl
 * @description
 * # AdministratorAdminCgwzCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
  .controller('AdministratorAdminCgwzCtrl', function ($scope, $rootScope, DataService, openApi, Util, $location, $routeParams, FormSubmit, localStorageService) {
    var id = ($scope.process_id = $routeParams.id);
    $scope.needEntryTypes = [
      {
        key: "不入库或使用[物资入库]流程来入库",
        value: 0
      },
      {
        key: "立即入库",
        value: 1
      }
    ];
    $scope.updatePriceTypes = [
      {
        key: "不需要",
        value: 0
      },
      {
        key: "需要",
        value: 1
      }
    ];
    $scope.needToReturnTypes = [
      {
        key: "否",
        value: 0
      },
      {
        key: "是",
        value: 1
      }
    ];
    // 选择报销单明细
    var getWZlist = function () {
      var param = {};
      param.type = "物资类型";
      Util.getKeyValue(param, function (data) {
        $scope.WZTypeList = data.data;
      });
    };
    $scope.showWZ = function () {
      $rootScope.$emit("showmasker");
      $scope.showCerPicker = true;
    };
    $scope.deletedItem = function (index) {
      $scope.param.paymentList.splice(index, 1);
      $scope.index = -1;
    };
    $scope.addWZPicker = function () {
      $scope.picker = $scope.picker || {};
      var check = false;
      check = checkPicker($scope.picker);
      if (!check) {
        return;
      }
      $scope.param.details.push($scope.picker);
      // 隐藏表单
      hideWZPicker();
    };
    var hideWZPicker = function () {
      $scope.picker = {};
      $rootScope.$emit("closemasker");
      $scope.showCerPicker = false;
    };
    $scope.$on("closemasker", function () {
      hideWZPicker();
    });
    $scope.cacelWZPicker = function () {
      hideWZPicker();
    };
    $scope.swipe = function (index) {
      $scope.paymentIndex = index;
    };
    $scope.resetlist = function (index) {
      $scope.paymentIndex = -1;
    };
    // 验证是否输入完整
    var checkPicker = function (data) {
      var check = true;
      if (!data.mname) {
        Util.Toast("请填写名称");
        check = false;
        return check;
      }
      if (!data.unit) {
        Util.Toast("请填写单位");
        check = false;
        return check;
      }
      if (!data.totalNum) {
        Util.Toast("请填写数量");
        check = false;
        return check;
      }
      if (typeof data.needToReturn ==="undefined") {
        Util.Toast("请选择是否需要归还");
        check = false;
        return check;
      }
      return check;
    };
    // 选择物资明细
    $scope.choosewz = function () {
      if (!$scope.param.materialType) {
        Util.Toast("请选择物资类型");
        return;
      }
      if ($scope.showCerPicker) {
        localStorageService.set("pickerdata", $scope.picker||{});
        hideWZPicker();
      }
      var relModuleObj = {
        materialType: $scope.param.materialType,
        flag: 1
      };
      Util.savaParamForReview($scope.param, id);
      Util.savaDataforsearch(relModuleObj);
      $location.path("administrator/choosewzk");
    };

    $scope.submit = function (operationId) {
      $scope.param.materialDetails = angular.toJson($scope.param.details);
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.buyMaterial;
      options.param = angular.copy($scope.param, {});
      delete options.param.details;
      FormSubmit.submit(options, $scope);
    };
    $scope.validate = function () {
      if (!$scope.param.materialType) {
        Util.Toast("请选择物资类型");
        return;
      }
      if (typeof $scope.param.needEntry ==="undefined") {
        Util.Toast("请选择是否入库");
        return;
      }

      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };
    // 删除证件信息列表
    $scope.deletedItem = function (index) {
      $scope.param.details.splice(index, 1);
      $scope.index = -1;
    };
    $scope.swipe = function (index) {
      $scope.index = index;
    };
    $scope.resetlist = function (index) {
      $scope.index = -1;
    };

    var mergeToPickerData = function () {
      var pickerdata = localStorageService.get("pickerdata");
      var selectLists = localStorageService.get("selectLists");
      localStorageService.remove("pickerdata");
      localStorageService.remove("selectLists");
      if (pickerdata) {
        var selectData = selectLists && selectLists[0];
        var data = {}
        if (selectData) {
          data = {
            mid: selectData.pkId,
            unit: selectData.unit,
            inventoryNum: selectData.availableCount,
            unitPrice: selectData.unitPrice,
            specifications: selectData.specifications,
            mname: selectData.materialName,
            needToReturn: selectData.needToReturn,
          }
        }
        $scope.picker = data;
        $scope.showWZ();
      }
    }

    var init = function () {
      // 获取回显的详细信息
      getWZlist();
      Util.getDetailData(id, function (data) {
        $scope.param = data;
        $scope.param.details = $scope.param.details || [];
        mergeToPickerData()
      });
    };
    // 初始化
    init();

  });
